"use strict"
const Controller = require("egg").Controller
const {sendStat} = require("../extend/helper")
class ModuleApplicatController extends Controller {
	constructor(ctx) {
		super(ctx)
		this.ModuleApplicat = this.app.model.ModuleApplicat
		this.Module = this.app.model.Module
	}
	async list() {
		let {ctx} = this
		ctx.validate({aid: "numstr"}, ctx.query)
		let data = await this.ModuleApplicat.findAll({
			include: [this.Module],
			where: {
				aid: ctx.query.aid
			}
		})
		this.ctx.body = sendStat(this.config.constant.NORMAL, data)
	}
	async updateStat() {
		this.app.cache.moduleApplicat = undefined
		let {ctx, service, config} = this
		let body = this.ctx.request.body
		ctx.validate({
			aid: "pintstr",
			mid: "pintstr",
			stat: "numstr"
		}, body)

		let updataStat = await service.moduleApplicat.changStat(Number(body.aid), Number(body.mid), body.stat)
		if (updataStat.stat) {
			ctx.body = sendStat(config.constant.NORMAL)
		} else {
			ctx.body = sendStat(config.constant.NORMAL, {msg: updataStat.msg})
		}
	}
}

module.exports = ModuleApplicatController